/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbKv-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_09_26			*/
/*	File:		GlbKvMulTest.cpp		*/
/********************************************************/

#include "GlbKv.h"

#define	GLB_KM_NUM			10000
#define	GLB_KM_MULTI			"238.6.10.12:20139"

int main(int argc, char* argv[])
{
	int iCount = 1;
	GlbNet::CGlbPro CPro;
	GlbCls::CGlbTime CTime;
	GlbNet::CGlbMulti CMulti;
	char szKey[GLB_PACKET] = { 0 };
	char szVal[GLB_PACKET] = { 0 };
	char szMulti[GLB_URL] = GLB_KM_MULTI;
	UCHAR uszPacket[GLB_KBYTES8] = { 0 };
	GlbNet::GLBPROKV_S stKv[GLB_KV_PAIR] = {{GLB_KV_KEY, 0, (UCHAR*)szKey}, {GLB_KV_VAL, 0, (UCHAR*)szVal}};
	GlbNet::GLBPROHEAD_S stHead = {
		GLB_PRO_FLAG,
		GLB_PRO_1,
		GLB_PRO_DATA,
		GLB_PRO_0,
		GLB_PRO_Y,
		GLB_PRO_0,
		GLB_PRO_0,
		GLB_DEF_INST,
		GLB_PRO_0,
		GLB_DEF_SUCC,
		GLB_KV_PAIR,
		GLB_PRO_0,
	};

	CTime.GlbTimeOn();

	for (; iCount < GLB_KM_NUM; iCount ++) {
		usleep(100);
		sprintf(szKey, "%d", iCount);
		sprintf(szVal, "Val: %d+", iCount);
		memset(szVal + strlen(szVal), 'A', GLB_BUFFER);

		stKv[0].m_iLength = strlen(szKey);
		stKv[1].m_iLength = strlen(szVal);

		CPro.GlbProPutBody(NULL);
		CPro.GlbProInduce(&stHead);
		CPro.GlbProPutLength(GlbProKvOutput(stKv, GLB_KV_PAIR, &uszPacket[GLB_PRO_HEAD], GLB_KBYTES8 - GLB_PRO_HEAD));
		CPro.GlbProCreate(uszPacket, GLB_KBYTES8);

		if (GLB_SUCCESS == CMulti.GlbNetInitSend(szMulti)) {
			if (CMulti.GlbNetSend(uszPacket, GLB_PRO_HEAD + CPro.GlbProGetLength()) > 0) {
				GLB_PRINT("Result[%08d]: success\n", iCount);
			}
			else {
				GLB_PRINT("Result[%08d]: failure\n", iCount);
			}
			CMulti.GlbNetClose();
		}
	}

	CTime.GlbTimeOff();
	CTime.GlbTimeDisplay();

	return GLB_SUCCESS;
}
